from django.shortcuts import render
from django.http import HttpResponse
from .models import *
from django.db import connection
from datetime import datetime
from django.utils.timezone import make_aware
# from django.utils.datetime_safe import datetime

# Create your views here.


def index(request):
    return HttpResponse('success')


def exact(request, id=1):
    a = Article.objects.get(id__exact=id)
    print(a)
    # print(a.query)
    print(connection.queries[-1])
    b = Article.objects.filter(id__exact=id)
    print(b)
    print(b.query)
    print(connection.queries[-1])
    a = Article.objects.filter(content__exact=None)
    print(a.count())
    print(connection.queries[-1])
    try:
        c = Article.objects.get(content__exact=None)
        print(c)
        print(connection.queries[-1])
    except Article.DoesNotExist as e:
        print(e)

    return HttpResponse(a)


def iexact(request, id=1):
    a = Article.objects.filter(title__iexact=id)
    print('='*30)
    print(a)
    print(connection.queries[-1])
    print('='*30)
    return HttpResponse('ok')


def contains(request, id=''):
    result = ['success']
    if id:
        a = Article.objects.filter(title__contains=id)
        result.append(a)
        print(a)
        print(a.query)
        result.append(a.query)
        a = Article.objects.filter(title__icontains=id)
        result.append(a)
        print(a)
        print(a.query)
        result.append(a.query)
    return HttpResponse(result)


def categoryinit(request):
    category = Category(name='热门')
    category.save()
    Article(title="热门文章1", content='猪肉降价了', category=category).save()
    Article(title="猪肉降价了", content='普天同庆', category=category).save()
    Article(title="疯狂降价了", content='今天大泽', category=category).save()
    Article(title="热门文章2", content='猪肉降价了', category=category).save()
    return HttpResponse('ok')


def in_view1(request):
    articles = Article.objects.filter(id__in=[1, 2, 3])
    print(articles)
    print(articles.query)
    print(connection.queries[-1])
    return HttpResponse('ok')


def in_view2(request):
    # 判断的是id
    categories = Category.objects.filter(article__in=[6, 7, 8])
    print(categories)
    print(categories.query)
    categories = Category.objects.filter(article__id__in=[6, 7, 8])
    print(categories)
    print(categories.query)
    for category in categories:
        print(category)
    articles = Article.objects.filter(title__icontains='猪')
    categories = Category.objects.filter(article__in=articles)
    print(articles)
    print(articles.query)
    print(categories)
    print(categories.query)
    print(connection.queries[-1])
    categories = Category.objects.filter(article__title__icontains='猪')
    print(categories)
    print(categories.query)
    print(connection.queries[-1])
    return HttpResponse('ok')


def gt(request):
    a = Article.objects.filter(id__gt=4)
    print(a)
    print(a.query)
    a = Article.objects.filter(id__gte=4)
    print(a)
    print(a.query)
    a = Article.objects.filter(id__lt=4)
    print(a)
    print(a.query)
    a = Article.objects.filter(id__lte=4)
    print(a)
    print(a.query)
    return HttpResponse('ok')


def startswith(request):
    a = Article.objects.filter(title__startswith='三')
    print(a)
    print(a.query)
    a = Article.objects.filter(title__istartswith='三')
    print(a)
    print(a.query)
    a = Article.objects.filter(title__endswith='三')
    print(a)
    print(a.query)
    a = Article.objects.filter(title__iendswith='三')
    print(a)
    print(a.query)
    return HttpResponse('ok')


def range(request):
    start_time = (datetime(2019, 12, 8))

    end_time = (datetime(2019, 12, 10))

    a = Article.objects.filter(created_at__range=(start_time, end_time))
    print(a)
    print(a.query)
    return HttpResponse('range')


def date(request):
    a = Article.objects.filter(created_at__date=datetime(2019, 12, 8))
    print(a)
    print(a.query)
    return HttpResponse('range')


def isnull(request):
    a = Article.objects.filter(content__isnull=False)
    print(a)
    print(a.query)
    a = Article.objects.filter(category_id__isnull=False)
    print(a)
    print(a.query)
    a = Article.objects.filter(category__isnull=False)
    print(a)
    print(a.query)
    return HttpResponse('isnull')


def re(request):
    a = Article.objects.filter(title__regex=r'^hello')
    print(a)
    print(a.query)
    a = Article.objects.filter(title__iregex=r'^hello')
    print(a)
    print(a.query)

    return HttpResponse('isnull')
